package com.xm.xmapp.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.xm.xmapp.R;
import com.xm.xmapp.gridpasswordview.GridPasswordView;
import com.xm.xmapp.gridpasswordview.GridPasswordView.OnPasswordChangedListener;

public class PilePasswordDialog extends Dialog {

	private Context context;
	private DissMissable mDissMissable;
	private GridPasswordView gridPasswordView;

	public interface DissMissable {
		void getPsw(String pwd);
	}

	public PilePasswordDialog(Context context ,DissMissable mDissMissable) {
		super(context, R.style.Dialog);
		this.context = context;
		this.mDissMissable = mDissMissable;
	}

	public void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_pile_password, null);
		gridPasswordView = (GridPasswordView) view.findViewById(R.id.pswView);
		gridPasswordView.setOnPasswordChangedListener(new OnPasswordChangedListener() {

			@Override
			public void onTextChanged(String psw) {

			}

			@Override
			public void onInputFinish(String psw) {
				dismiss();
				if (mDissMissable != null)
					mDissMissable.getPsw(psw);
			}
		});
		view.findViewById(R.id.diss).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		setContentView(view);
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
		dialogWindow.setAttributes(lp);
	}

	@Override
	public void show() {
		super.show();

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				gridPasswordView.forceInputViewGetFocus();
			}
		}, 200);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}
}
